<template>
  <el-form>
    <el-form-item label="角色名称">
      <el-input v-model="role.name"></el-input>
    </el-form-item>
    <el-form-item label="角色编码">
      <el-input v-model="role.code"></el-input>
    </el-form-item>
    <el-form-item label="角色描述">
      <el-input type="textarea" v-model="role.description"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button @click="onCancel">取消</el-button>
      <el-button type="primary" @click="onSubmit">确认</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { createOrUpdate, getRoleById } from '@/services/role.js'

export default {
  name: 'CreateOrEdit',
  props: {
    isEdit: {
      type: Boolean,
      default: false
    },
    roleId: {
      type: [Number, String]
    }
  },
  created () {
    if (this.isEdit) {
      console.log(this.roleId)
      this.loadRole()
    }
  },
  data () {
    return {
      role: {
        name: '',
        code: '',
        description: ''
      }
    }
  },
  methods: {
    // 确定按钮
    async onSubmit () {
      const { data } = await createOrUpdate(this.role)
      if (data.code === '000000') {
        this.$message.success(this.isEdit ? '编辑成功' : '添加成功')
        this.role = {}
        this.$emit('success')
      }
    },
    // 取消按钮
    onCancel () {
      this.$emit('cancel')
      this.role = {}
    },
    // 编辑显示角色数据
    async loadRole () {
      const { data } = await getRoleById(this.roleId)
      if (data.code === '000000') {
        this.role = data.data
      }
    }
  }
}
</script>

<style lang="scss" scoped></style>
